package com.mayikt;

/**
 * @Name Test05
 * @Author 枫火
 * @Date 2022/8/31 8:02
 * @Description
 **/
class ThreadDemo extends Thread {

    public boolean flag = true;

    //public volatile boolean flag = true;

    @Override
    public void run() {
        System.out.println("线程开始");
        while (flag) {

        }
        System.out.println("线程结束");
    }

    public void setRunning(boolean flag) {
        this.flag = flag;
    }

}

public class Test05 {
    public static void main(String[] args) throws InterruptedException {
        ThreadDemo threadDemo = new ThreadDemo();
        threadDemo.start();
        Thread.sleep(1000);
        threadDemo.setRunning(false);
        System.out.println("flag已经改为false");
        Thread.sleep(1000);
        System.out.println("flag:" + threadDemo.flag);
    }

}
